//
//  TimerViewController.m
//  CatMusic
//
//  Created by dllo on 15/5/2.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "TimerViewController.h"
#import "Singleton.h"
#import "PlayerSingleton.h"
#define HEIGHT self.view.frame.size.height
#define WIDTH self.view.frame.size.width
@interface TimerViewController ()<UITextFieldDelegate>
//@property(nonatomic, assign)int second;
//@property(nonatomic, retain) NSTimer *timer;
@property(nonatomic, retain)UIButton *button;
@property(nonatomic, retain)UILabel *textLabel;
@property(nonatomic, retain)UITextField *timeField;

@end

@implementation TimerViewController

- (void)dealloc
{
    [_button release];
    //    [_timer release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [backImageView setImage:[UIImage imageNamed:@"timeBackground"]];
    [self.view addSubview:backImageView];
    
    
    NSMutableArray *array = [NSMutableArray array];
    [array addObject:@"30分钟"];
    [array addObject:@"60分钟"];
    [array addObject:@"90分钟"];
    [array addObject:@"120分钟"];
    
    
    
    
    [Singleton shareSingleton].segment = [[UISegmentedControl alloc] initWithItems:array];
    [self.view addSubview:[Singleton shareSingleton].segment];
    [[Singleton shareSingleton].segment setFrame:CGRectMake(WIDTH / 375 * 30, HEIGHT /667 * 140, WIDTH / 375 *300, HEIGHT /667 *40)];
    [[Singleton shareSingleton].segment addTarget:self action:@selector(setTime:) forControlEvents:UIControlEventValueChanged];
//    [segment release];
    
    
    self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 375 * 100, HEIGHT /667 * 50, WIDTH / 375 * 200, HEIGHT /667 * 30)];
    self.textLabel.textColor = [UIColor blueColor];
    self.textLabel.text = @"离休眠时间还有~";
    [self.view addSubview:self.textLabel];
    
    
    
    
    [Singleton shareSingleton].timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 375 * 60, HEIGHT /667 * 60, WIDTH / 375 * 200, HEIGHT /667 * 60)];
//    [[Singleton shareSingleton].timeLabel setBackgroundColor:[UIColor purpleColor]];
    [[Singleton shareSingleton].timeLabel setTextColor:[UIColor orangeColor]];
    [self.view addSubview:[Singleton shareSingleton].timeLabel];
    [Singleton shareSingleton].timeLabel.textAlignment = NSTextAlignmentCenter;
    [Singleton shareSingleton].timeLabel.font = [UIFont systemFontOfSize:22];
    NSLog(@"%@, %d", [Singleton shareSingleton].timeLabel.text, __LINE__);
    


    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 375 * 30, HEIGHT /667 * 200, WIDTH / 375 * 90, HEIGHT /667 * 20)];
    [label setText:@"自定义时间:"];
  
    [self.view addSubview:label];
    
    
    
    self.timeField = [[UITextField alloc] initWithFrame:CGRectMake(WIDTH / 375 * 120, HEIGHT /667 * 200, WIDTH / 375 * 200, HEIGHT /667 * 20)];
    self.timeField.borderStyle = UITextBorderStyleRoundedRect;
    self.timeField.keyboardType = UIKeyboardTypeNumberPad;
//    self.timeField.backgroundColor = [UIColor clearColor];
    self.timeField.placeholder = @"请输入时间(分钟)";
    [self.view addSubview:self.timeField];
    
    
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.button setFrame:CGRectMake(WIDTH / 375 * 150, HEIGHT /667 * 250, WIDTH / 375 * 80, HEIGHT /667 * 40)];
    [self.button setBackgroundColor:[UIColor clearColor]];
    [self.button setTitle:@"开始" forState:UIControlStateNormal];
    self.button.layer.borderWidth = 1;
    [self.view addSubview:self.button];
    [self.button addTarget:self action:@selector(buttonStart) forControlEvents:UIControlEventTouchUpInside];
    
    
}


- (void)setTime:(UISegmentedControl *)sender
{
    
    switch (sender.selectedSegmentIndex) {
        case 0:
            [Singleton shareSingleton].second = 30 * 60;
            break;
        case 1:
            [Singleton shareSingleton].second = 60 * 60;
            break;
        case 2:
            [Singleton shareSingleton].second = 90 * 60;
            break;
        case 3:
            [Singleton shareSingleton].second = 120 * 60;
            break;
        default:
            break;
    }
    
}


- (void)buttonStart
{
    
    
    [Singleton shareSingleton].clickStatu = ![Singleton shareSingleton].clickStatu;
    
    if ([Singleton shareSingleton].clickStatu) {
        //改变图片
        [self.button setTitle:@"暂停" forState:UIControlStateNormal];
        if (![self.timeField.text isEqualToString:@""]) {
            [Singleton shareSingleton].second = self.timeField.text.intValue * 60;///
            [self.timeField resignFirstResponder];
            self.timeField.text = @"";
            
        }
        if ([Singleton shareSingleton].second > 0) {
            [Singleton shareSingleton].timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(lastTime) userInfo:nil repeats:YES];//计时器
            [Singleton shareSingleton].timeLabel.hidden = NO;
            self.textLabel.hidden = NO;
        }
        NSLog(@"开始");
    } else {
        [self.button setTitle:@"开始" forState:UIControlStateNormal];
        
        //        [self.timer invalidate];
        //        [[Singleton shareSingleton].timer invalidate];
        
        //        [Singleton shareSingleton].timer = nil;
        
        [[Singleton shareSingleton].timer setFireDate:[NSDate distantFuture]];
        [Singleton shareSingleton].timeLabel.hidden = NO;
        self.textLabel.hidden = YES;
        NSLog(@"暂停");
    }
    
    
}

- (void)lastTime
{
    
    int hour = (int)([Singleton shareSingleton].second/3600);
    int minute = (int)([Singleton shareSingleton].second - hour*3600)/60;
    int second = [Singleton shareSingleton].second - hour*3600 - minute*60;
    [Singleton shareSingleton].timeLabel.text = [NSString stringWithFormat:@"%d时%d分%d秒", hour, minute,second];
//    NSLog(@"%@", [Singleton shareSingleton].timeLabel.text);
    [Singleton shareSingleton].second--;
    if ([[Singleton shareSingleton].timeLabel.text isEqualToString:@"0时0分0秒"]) {
        [[PlayerSingleton sharePlayerSingleton].player pause];
        [[Singleton shareSingleton].timer setFireDate:[NSDate distantFuture]];
        [Singleton shareSingleton].timeLabel.hidden = YES;
    }
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.timeField resignFirstResponder];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
